package com.zhongxi.guoyuan.adapter.beautifulStore;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.beautifulStore.BeautifulStoreData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.beautifulStore.StoreDetailActivity;

import java.io.LineNumberInputStream;
import java.util.List;

/**
 * Created by Administrator on 2016/11/11.
 */

public class BeautifulStoreAdapter extends BaseAdapter {

    private final LayoutInflater mLayoutInflater;
    private List<BeautifulStoreData> data;
    private Context context;

    private int temp = -1;

    public BeautifulStoreAdapter(Context context, List<BeautifulStoreData> data) {
        this.data = data;
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_beautiful_store, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivShopImg);
                holder.checkBox = (ImageView) convertView.findViewById(R.id.checkbox);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvShopName);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.tvShopAddress);
                holder.tvDistance = (TextView) convertView.findViewById(R.id.tvDistance);
                holder.ratingBar= (RatingBar) convertView.findViewById(R.id.ratingBar);
                holder.ratingbar_score= (TextView) convertView.findViewById(R.id.ratingbar_score);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StoreDetailActivity.class);
                    intent.putExtra("shopid", data.get(position).getId());
                    context.startActivity(intent);
                }
            });
            BeautifulStoreData list = data.get(position);
            if (null != list.getThumbnail() && !"".equals(list.getThumbnail())) {
                Glide.with(context).load(Url.IMG_URL + list.getThumbnail()).placeholder(R.mipmap.iv_eg).into(holder.ivImg);
            }
            if (null != list.getName() && !"".equals(list.getName())) {
                holder.tvName.setText(list.getName());
            } else {
                holder.tvName.setText(R.string.app_name);
            }

            if(null!=list.getStarlevel()&&!"".equals(list.getStarlevel())){
                holder.ratingBar.setRating(Float.parseFloat(list.getStarlevel()));
                holder.ratingbar_score.setText(list.getStarlevel());
            }else{
                holder.ratingBar.setRating(0);
                holder.ratingbar_score.setText("");
            }
            if (null != list.getAddress() && !"".equals(list.getAddress())) {
                holder.tvAddress.setText(list.getAddress());
            } else {
                holder.tvAddress.setText("");
            }
            if (temp != -1 && position == temp) {
                holder.checkBox.setImageResource(R.mipmap.iv_select);
            } else {
                holder.checkBox.setImageResource(R.mipmap.iv_noselect);
            }

            if(null!=list.getLongitude()&&!"".equals(list.getLongitude())&&null!=list.getDimensions()&&!"".equals(list.getDimensions())
                    &&null!=MyApplication.getInstance().getLongitude()&&null!=MyApplication.getInstance().getLatitude()
                    &&!"".equals(MyApplication.getInstance().getLatitude())&&!"".equals(MyApplication.getInstance().getLongitude())){
                holder.tvDistance.setText(CommonTool.getDistance(Double.parseDouble(list.getLongitude()),
                        Double.parseDouble(list.getDimensions()),
                        Double.parseDouble(MyApplication.getInstance().getLongitude()),
                        Double.parseDouble(MyApplication.getInstance().getLatitude())));
            }else{
                holder.tvDistance.setText("定位失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvAddress;
        private TextView tvDistance;
        private ImageView checkBox;
        private RatingBar ratingBar;
        private TextView ratingbar_score;
    }
}
